//
//  core.cpp
//  RPG
//
//  Created by Matt Bailey on 03/01/2014.
//  Copyright (c) 2014 Matt Bailey. All rights reserved.
//

#include "core.hpp"

string loadFile(string fileName) {
    
    string source;
    
    ifstream sourceStream;
    sourceStream.open(fileName);
    
    if (sourceStream.is_open()) {
        string line;
        
        while(getline(sourceStream, line)) {
            
            line.erase(remove_if(line.begin(), line.end(), ::isspace ), line.end() );
            
            // Comment Parsing
            unsigned long pos = line.find("//");
            
            if (pos != string::npos) {
                line.erase(pos, string::npos);
            }
            
            if (line != "") {
                source += "\n" + line;
            }
        }
        
        sourceStream.close();
    }
    
    else {
        
        cout << "\nERROR: Cannot open '" << fileName << "'\n";
        exit(EXIT_FAILURE);
    }

    // Remove = and ""
    replace(source.begin(), source.end(), '=', ' ');
    source.erase(remove(source.begin(), source.end(), '"' ), source.end() );

    return source;
}

string trim(string stringName, size_t start, size_t end) {
    
    stringName = stringName.substr(start, stringName.length());
    stringName = stringName.substr(0, end);
    
    return stringName;
}